﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DotNetX
{
    public static class StringReaderExtensions
    {
        public static string ReadWord(this StringReader reader)
        {
            string word = String.Empty;

            // skip the initial white space
            while (true)
            {
                char c = (char)reader.Peek();
                if (Char.IsWhiteSpace(c))
                    reader.Read();
                else
                    break;
            }

            while (true)
            {
                int i = reader.Peek();
                if (i == -1)
                    break;

                char c = (char)i;

                if (!Char.IsLetter(c) && !Char.IsNumber(c))
                    break;

                word += c;

                reader.Read(); // read the character just peeked
            }

            return word;
        }

        public static string ReadUntilCharacter(this StringReader reader, char c)
        {
            string line = String.Empty;

            // skip the initial white space
            while (true)
            {
                char cc = (char)reader.Peek();
                if (Char.IsWhiteSpace(cc))
                    reader.Read();
                else
                    break;
            }

            while (true)
            {
                int i = reader.Peek();
                if (i == -1)
                    break;

                char ch = (char)i;
                if (ch == c)
                    break;

                line += ch;

                reader.Read(); // read the character just peeked
            }

            return line;
        }
    }
}
